package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
/*
 * Main构建了主菜单界面以及管理员登录界面
 */
public class Main extends JFrame {
	private JPanel managerLoginPanel;
	private JPanel menuPanel;
	private JPanel managerPanel;

	public Main() {
		super("欢迎使用景区信息管理系统");
		setSize(515, 450);
		setLocation(600, 250);

		Font font = new Font("宋体", Font.PLAIN, 25);
		UIManager.put("OptionPane.font", font);
		UIManager.put("OptionPane.messageFont", font);
		UIManager.put("OptionPane.buttonFont", font);

		setVisible(true);
		menuPanel();
	}

	public static void main(String[] args) {
		new Main();
	}

	private void menuPanel() {
		if (managerLoginPanel != null) {
			remove(managerLoginPanel);
		}
		if (managerPanel != null) {
			remove(managerPanel);
		}

		menuPanel = new JPanel();
		menuPanel.setLayout(null);

		JButton button1 = new JButton("1.景区景点分布图");
		button1.setBounds(70, 30, 350, 40);
		button1.setFont(new Font("宋体", Font.PLAIN, 25));
		button1.setHorizontalAlignment(SwingConstants.LEFT);
		menuPanel.add(button1);
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ScenicsMatrix scenicsMatrix = new ScenicsMatrix();
				scenicsMatrix.scenicsMatrixPanel();
			}
		});

		JButton button2 = new JButton("2.景点的查找与排序");
		button2.setBounds(70, 70, 350, 40);
		button2.setFont(new Font("宋体", Font.PLAIN, 25));
		button2.setHorizontalAlignment(SwingConstants.LEFT);
		menuPanel.add(button2);
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ScenicsFindAndSort scenicsFindAndSort = new ScenicsFindAndSort();
				scenicsFindAndSort.scenicsFindAndSortPanel();
			}
		});

		JButton button3 = new JButton("3.输出导游线路图");
		button3.setBounds(70, 110, 350, 40);
		button3.setFont(new Font("宋体", Font.PLAIN, 25));
		button3.setHorizontalAlignment(SwingConstants.LEFT);
		menuPanel.add(button3);
		button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RouteMap routeMap = new RouteMap();
				routeMap.routeMapPanel();
			}
		});

		JButton button4 = new JButton("4.求最短路径");
		button4.setBounds(70, 150, 350, 40);
		button4.setFont(new Font("宋体", Font.PLAIN, 25));
		button4.setHorizontalAlignment(SwingConstants.LEFT);
		menuPanel.add(button4);
		button4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ShortestPath shortestPath = new ShortestPath();
				shortestPath.shortestPathPanel();
			}
		});

		JButton button5 = new JButton("5.停车场车辆记录信息");
		button5.setBounds(70, 190, 350, 40);
		button5.setFont(new Font("宋体", Font.PLAIN, 25));
		button5.setHorizontalAlignment(SwingConstants.LEFT);
		menuPanel.add(button5);
		button5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ParkAndSidewalk parkAndSidewalk = new ParkAndSidewalk();
				parkAndSidewalk.parkAndSidewalkPanel();
			}
		});

		JButton button6 = new JButton("6.浏览景区通告");
		button6.setBounds(70, 230, 350, 40);
		button6.setFont(new Font("宋体", Font.PLAIN, 25));
		button6.setHorizontalAlignment(SwingConstants.LEFT);
		menuPanel.add(button6);
		button6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BrowseNotice browseNotice = new BrowseNotice();
				browseNotice.browseNoticePanel();
			}
		});

		JButton button7 = new JButton("7.管理员登录");
		button7.setBounds(70, 270, 350, 40);
		button7.setFont(new Font("宋体", Font.PLAIN, 25));
		button7.setHorizontalAlignment(SwingConstants.LEFT);
		menuPanel.add(button7);
		button7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				managerLoginPanel();
			}
		});

		menuPanel.setVisible(true);
		add(menuPanel);
		setVisible(true);
	}
	//管理员登录界面
	private void managerLoginPanel() {
		if (menuPanel != null) {
			remove(menuPanel);
		}
		
		if (managerPanel != null) {
			remove(managerPanel);
		}
		
		managerLoginPanel = new JPanel();
		managerLoginPanel.setLayout(null);

		JLabel userLabel = new JLabel("用户名");
		userLabel.setBounds(70, 80, 150, 35);
		userLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerLoginPanel.add(userLabel);

		JTextField userText = new JTextField(20);
		userText.setBounds(220, 80, 200, 40);
		userText.setFont(new Font("宋体", Font.PLAIN, 25));
		managerLoginPanel.add(userText);

		JLabel passwordLabel = new JLabel("密码");
		passwordLabel.setBounds(70, 140, 150, 35);
		passwordLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerLoginPanel.add(passwordLabel);

		JPasswordField passwordText = new JPasswordField(20);
		passwordText.setBounds(220, 140, 200, 40);
		passwordText.setFont(new Font("宋体", Font.PLAIN, 25));
		managerLoginPanel.add(passwordText);

		JButton loginButton = new JButton("登录");
		loginButton.setBounds(350, 220, 100, 40);
		loginButton.setFont(new Font("宋体", Font.PLAIN, 25));
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				char[] pass=passwordText.getPassword();
				String password="";
				for(int i=0;i<pass.length;i++) {
					password+=pass[i];
				}
				/*管理员登录，点击登录按钮，系统调用manager类的match方法
				 * 若用户名密码匹配则进入管理界面
				 * 若不匹配则提示*/
				function.Manager manager=new function.Manager();
				boolean login = manager.match(userText.getText(), password);
				if (login) {
					managerPanel(userText.getText());
				} else {
					JOptionPane.showMessageDialog(null, "用户名和密码不匹配", "错误",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		//返回上一步的返回按钮，提升用户体验
		managerLoginPanel.add(loginButton);
		JButton returnButton = new JButton("<-");
		returnButton.setBounds(0, 0, 80, 40);
		returnButton.setFont(new Font("宋体", Font.PLAIN, 25));
		returnButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuPanel();
			}
		});
		managerLoginPanel.add(returnButton);

		managerLoginPanel.setVisible(true);
		add(managerLoginPanel);
		setVisible(true);
	}
	//若登录成功则进入管理员界面
	private void managerPanel(String manager) {
		if (managerLoginPanel != null) {
			remove(managerLoginPanel);
		}

		managerPanel = new JPanel();
		managerPanel.setLayout(null);

		JButton button1 = new JButton("1.管理景点");
		button1.setBounds(100, 70, 300, 50);
		button1.setFont(new Font("宋体", Font.PLAIN, 25));
		button1.setHorizontalAlignment(SwingConstants.LEFT);
		managerPanel.add(button1);
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ManagerScenic managerScenic = new ManagerScenic();
				managerScenic.managerScenicPanel();
			}
		});

		JButton button2 = new JButton("2.管理道路");
		button2.setBounds(100, 140, 300, 50);
		button2.setFont(new Font("宋体", Font.PLAIN, 25));
		button2.setHorizontalAlignment(SwingConstants.LEFT);
		managerPanel.add(button2);
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ManagerEdge managerEdge = new ManagerEdge();
				managerEdge.managerEdgePanel();
			}
		});

		JButton button3 = new JButton("3.发布公告");
		button3.setBounds(100, 210, 300, 50);
		button3.setFont(new Font("宋体", Font.PLAIN, 25));
		button3.setHorizontalAlignment(SwingConstants.LEFT);
		managerPanel.add(button3);
		button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ManagerNotice managerNotice = new ManagerNotice();
				managerNotice.managerNoticePanel(manager);
			}
		});
		//返回上一步的返回按钮，提升用户体验
		JButton returnButton = new JButton("<-");
		returnButton.setBounds(0, 0, 80, 40);
		returnButton.setFont(new Font("宋体", Font.PLAIN, 25));
		returnButton.setHorizontalAlignment(SwingConstants.LEFT);
		managerPanel.add(returnButton);
		returnButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				managerLoginPanel();
			}
		});

		managerPanel.setVisible(true);
		add(managerPanel);
		setVisible(true);
	}
}
